//
//  twittsTableViewController.m
//  twittsTab2
//
//  Created by Alexander Samarchi on 3/8/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "twittsTableViewController.h"
#import "TwitterRequest.h"
#import "twittsTab2AppDelegate.h"


@implementation twittsTableViewController
@synthesize listData, tbl;
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidLoad {
	//NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
	//NSString *username;
	//NSString *password;
	
	TwitterRequest *friends = [[TwitterRequest alloc] init];
	
	/*if (standardUserDefaults){
		username = [standardUserDefaults objectForKey:@"username"];
		password = [standardUserDefaults objectForKey:@"password"];
	}*/
	
	friends.username = @"CHCProject";
	friends.password = @"team22";
	stories = [[NSMutableArray alloc] init];
	NSLog(@"Getting timeline for user %@", friends.username);
	
	[friends friends_timeline:self requestSelector:@selector(friends_timeline_callback:)];
	
	
    // Override point for customization after app launch    
	//NSLog(@"%@",stories);
	
	[super viewDidLoad];
}

- (void)friends_timeline_callback:(NSData *)data{
	NSString *timeline = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
	
	timelineXML = [[NSXMLParser alloc] initWithData:data];
	
	[timelineXML setDelegate:self];
	BOOL success = [timelineXML parse];
	
    if(success)
		NSLog(@"no errors");
	else
		NSLog(@"Errors Errors");
	NSLog(@"%@",stories);
	[tbl reloadData];
	//NSLog(@"Timeline %@", timeline);
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
	attributes:(NSDictionary *)attributeDict {
	currentElement = [elementName copy];
	if([elementName isEqualToString:@"status"]){
		item = [[NSMutableDictionary alloc] init];
		currentTitle = [[NSMutableString alloc] init];
	}
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
	if([currentElement isEqualToString:@"text"]){
		[currentTitle appendString:string];
	}
	
	
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
	if([elementName isEqualToString:@"status"])
	{
		[item setObject:currentTitle forKey:@"text"];
		[stories addObject:[item objectForKey:@"text"]];
		//NSLog(@"adding status: %@", currentTitle);
		//NSLog(@"%@",stories);
	}
	
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	NSLog(@"table count");
	return [stories count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
	if(cell == nil)
	{
		cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
	}
	NSUInteger row = [indexPath row];
	cell.textLabel.text = [stories objectAtIndex:row];
	return cell;
}

- (void)dealloc {
    [super dealloc];
}


@end
